package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NursingServiceImpl
 * @Author fy
 * @Date 2025-05-07  19:56
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    /**
     * 分页条件查询护理项目
     *
     * @param
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> nursingProjects = nursingProjectMapper.selectByPage(name,status);
        PageResponse<NursingProjectVo> nursingProjectPageInfo = PageResponse.of(nursingProjects, NursingProjectVo.class);
        return nursingProjectPageInfo;

    }

    /**
     * 新增护理项目
     */
    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getNursingProjectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getNursingProjectById(id);
        //使用 BeanUtil.toBean 方法将 NursingProject 实体对象转换为 NursingProjectVo 视图对象（VO），用于向前端返回数据时屏蔽不必要的字段或进行数据格式转换。
       return BeanUtil.toBean(nursingProject,NursingProjectVo.class);

    }

    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtil.copyProperties(nursingProjectDto,nursingProject);
        Long id = nursingProject.getId();
        NursingProject nursingId = nursingProjectMapper.getNursingProjectById(id);
        Integer count =nursingId.getCount();
        if (count !=0){
            throw new BaseException(BasicEnum.Update_EXCEPTION);
        }
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void deleteNursingProject(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getNursingProjectById(id);
        Integer count = nursingProject.getCount();
        if (count !=0){
            throw new BaseException(BasicEnum.DELETE_EXCEPTION);
        }
        nursingProjectMapper.deleteNursingProject(id);
    }

    /**
     * 根据id修改护理项目状态
     *
     * @param id
     */
    @Override
    public void updateStatus(Long id) {
        // 1. 查询当前护理项目信息
        NursingProject nursingProject = nursingProjectMapper.getNursingProjectById(id);
        if (nursingProject.getCount()!=0 && nursingProject.getStatus() ==1){
            throw new BaseException(BasicEnum.Update_EXCEPTION);
        }
        // 2. 切换状态（0: 禁用, 1: 启用）
        int currentStatus = nursingProject.getStatus();
        nursingProject.setStatus(currentStatus == 1 ? 0 : 1); // 取反切换状态
        // 3. 执行更新操作
        nursingProjectMapper.updateNursingProject(nursingProject);

    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> getAllNursingProject() {
        List<NursingProject> nursingProjects =nursingProjectMapper.getAllNursingProject();
        return BeanUtil.copyToList(nursingProjects, NursingProjectVo.class);
    }

}
